package com.sita.tingterest.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;

import com.sita.tingterest.services.MusicService;


public abstract class BaseActivity extends Activity {
    protected MusicService mPlayService;
    public static BaseActivity baseActivity;


    private ServiceConnection mPlayServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i("MY TAG", "onServiceDisconnected");
            mPlayService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mPlayService = ((MusicService.MyMusicBinder) service).getServices();
            mPlayService.setOnMusicEventListener(mMusicEventListener);
            Log.i("MY TAG", "onServiceConnected");
            //  onChange(mPlayService.getPlayingPosition());
        }
    };

    public void allowBindService() {
        Log.i("MY TAG", "allowBindService()");
        bindService(new Intent(getApplicationContext(), MusicService.class), mPlayServiceConnection,
                Context.BIND_AUTO_CREATE);
    }

    /**
     * fragment的view消失后回调
     */
    public void allowUnbindService() {
        Log.i("MY TAG", "allowUnbindService()");
        unbindService(mPlayServiceConnection);
    }

    private MusicService.OnMusicEventListener mMusicEventListener =
            new MusicService.OnMusicEventListener() {
                @Override
                public void onPublish(int progress) {
                    Log.i("MY TAG", "onPublish in MainAcrtivity " + progress);

                    //    mediaFileLengthInMilliseconds=mPlayService.getDuration();
                    //       seekBarProgress.setProgress((int) (((float)progress/ mediaFileLengthInMilliseconds) * 100)); // This math construction give a percentage of "was playing"/"song length"
///                  music_currwnt_position.setText(String.valueOf((int) (((float)progress/ mediaFileLengthInMilliseconds)*100)));
                    //   music_Total_length.setText(String.valueOf(mediaFileLengthInMilliseconds));
                    BaseActivity.this.onPublish(progress);
                }

                @Override
                public void onChange(int position) {
                    Log.i("MY TAG", "onChange :" + position);
                    BaseActivity.this.onChange(position);
                }
            };


    protected abstract void onChange(int position);

    protected abstract void onPublish(int progress);


}